package com.xiaomi.controller;

import com.github.pagehelper.PageInfo;
import com.xiaomi.pojo.Goods;
import com.xiaomi.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName GoodsService
 * @Author xiao宋
 * @Date 11:50  2022/9/14
 * @Version 1.0
 * @Description TODO
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     * 通过 ID 获取商品列表
     * @return TODO 分页问题暂时搁置，先弄别的
     */
//    @ResponseBody
    @RequestMapping("/getGoodsListByTypeId")
    public ModelAndView getGoodsListByTypeId(
            ModelAndView mav, int goodsTypeId, HttpServletRequest req,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "3") Integer pageSize
    ) {
        System.out.println("获取商品列表--前端传过来的商品id" + goodsTypeId);

        PageInfo<Goods> pageInfo = goodsService.findGoodsListByTypeIdPage(goodsTypeId,pageNum, pageSize);

        System.out.println("输出分页数据" + pageInfo);
        req.setAttribute("pageBean", pageInfo);

        mav.addObject("pageBean", pageInfo);
//        mav.setViewName("redirect:/goodsList.jsp");
        mav.setViewName("forward:/goodsList.jsp");
        return mav;
    }

    /**
     * 通过 ID 获取商品
     *
     * @param req
     * @return
     */
    @RequestMapping("/getGoodsById")
    public String getGoodsById(int id, HttpServletRequest req) {
        System.out.println("前端显示商品处传过来的id：" + id);
        Goods goods = goodsService.findById(id);
        System.out.println("数据库根据id查到的商品名： " + goods.getName());
        req.setAttribute("goods", goods);

//        return "forward:/goodsDetail.jsp";
        return "redirect:/goodsDetail.jsp";
    }
    @RequestMapping("/adminAddGoods")
    public String addGoods(
            String name,
            String picture,
            int price,
            int star,
            int typeid,
            String pubdate,
            String intro) {
        // 名字。图片 价格 评分 typeid  不能是空
        //从前端拿到数据
        if (name == null || picture == null) {
            System.err.println("添加商品失败。商品名与图片不能为空");
            return "forward:/admin/addGoods.jsp";
        }
        // 可以是空 pubdate.intro
        if (intro.length() == 0) {
            intro="0";
        }
        if (pubdate.length() == 0 ){
            pubdate="0";
        }
        goodsService.addGoods(name,picture,intro,price,star,pubdate,typeid);
//        return "forward:/admin/addGoods.jsp";
        return "forward:/admin/showGoods.jsp";
    }
    /**
     * 查看商品数量   商品 数量
     * @return
     */
    @RequestMapping("/adminGetGoodsList")
    public ModelAndView getGoodsList() {
        List<Goods> goodsList = goodsService.getGoodsList();
        ModelAndView mv = new ModelAndView();
        if (goodsList != null) {
            mv.addObject("goodsList", goodsList);
        }
        mv.setViewName("forward:/admin/showGoods.jsp");
        return mv;
    }

}
